﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_Admin_CaiDatThamSoVanBan : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            layDanhSachLoaiVanBan();

            layDanhSachPhamVi();

            layDanhSachTrangThai();

            layDanhSachTinhChat();

            layDanhSachHinhThuc();

            layDanhSachNoiPhatHanh();
        }
    }

    //*****Table LoaiVanBan*****
    protected void layDanhSachLoaiVanBan()
    {
        LoaiVanBanBUS lvbBUS = new LoaiVanBanBUS();
        gvLoaiVanBan.DataSource = lvbBUS.layDanhSachLoaiVanBan();
        gvLoaiVanBan.DataBind();
    }
    protected void btnThemLoaiVanBan_Click(object sender, EventArgs e)
    {
        LoaiVanBan lvbDTO = new LoaiVanBan();
        lvbDTO.MaLoaiVanBan =txtMaLoaiVanBan.Text.ToUpper();
        lvbDTO.TenLoaiVanBan = txtTenLoaiVanBan.Text.ToUpper();

        //Kiem tra rong
        if (lvbDTO.MaLoaiVanBan.ToString() == string.Empty)
        {
            Alert.Show("Ma loai van ban khong duoc rong");
            return;
        }

        if (lvbDTO.TenLoaiVanBan.ToString() == string.Empty)
        {
            Alert.Show("Ten loai van ban khong duoc rong");
            return;
        }

        //Them
        if (lvbDTO.MaLoaiVanBan.ToString() != string.Empty && lvbDTO.TenLoaiVanBan.ToString() != string.Empty)
        {
            LoaiVanBanBUS lvbBUS = new LoaiVanBanBUS();

            if (lvbBUS.kiemtraTonTaiMaLoaiVanBan(lvbDTO.MaLoaiVanBan.ToUpper().ToString()) == false)
            {
                if (lvbBUS.kiemtraTonTaiTenLoaiVanBan(lvbDTO.TenLoaiVanBan.ToUpper().ToString()) == false)
                {
                    lvbBUS.themLoaiVanBan(lvbDTO);

                    layDanhSachLoaiVanBan();

                    MyTextBox myTextBox = new MyTextBox();
                    myTextBox.Xoa(txtMaLoaiVanBan);
                    myTextBox.Xoa(txtTenLoaiVanBan);
                }
                else
                {
                    Alert.Show("Ten loai van ban nay da ton tai");
                    return;
                }
            }
            else
            {
                Alert.Show("Ma loai van ban nay da ton tai");
                return;
            }
        }
    }
    protected void gvLoaiVanBan_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvLoaiVanBan.PageIndex = e.NewPageIndex;

        layDanhSachLoaiVanBan();
    }
    protected void btnXoaLoaiVanBan_Click(object sender, EventArgs e)
    {
        LoaiVanBanBUS lvbBUS = new LoaiVanBanBUS();

        lvbBUS.xoaLoaiVanBan(gvLoaiVanBan);
    }
    //*****

    //*****Table PhamVi*****
    protected void layDanhSachPhamVi()
    {
        PhamViBUS pvBUS = new PhamViBUS();
        gvPhamVi.DataSource = pvBUS.layDanhSachPhamVi();
        gvPhamVi.DataBind();
    }
    protected void btnThemPhamVi_Click(object sender, EventArgs e)
    {
        PhamVi pvDTO = new PhamVi();
        pvDTO.MaPhamVi = txtMaPhamVi.Text.ToUpper();
        pvDTO.TenPhamVi = txtTenPhamVi.Text.ToUpper();

        //Kiem tra rong
        if (pvDTO.MaPhamVi.ToString() == string.Empty)
        {
            Alert.Show("Ma pham vi khong duoc rong");
            return;
        }

        if (pvDTO.TenPhamVi.ToString() == string.Empty)
        {
            Alert.Show("Ten pham vi khong duoc rong");
            return;
        }

        //Them
        if (pvDTO.MaPhamVi.ToString() != string.Empty && pvDTO.TenPhamVi.ToString() != string.Empty)
        {
            PhamViBUS pvBUS = new PhamViBUS();

            if (pvBUS.kiemtraTonTaiMaPhamVi(pvDTO.MaPhamVi.ToUpper().ToString()) == false)
            {
                if (pvBUS.kiemtraTonTaiTenPhamVi(pvDTO.TenPhamVi.ToUpper().ToString()) == false)
                {
                    pvBUS.themPhamVi(pvDTO);

                    layDanhSachPhamVi();

                    MyTextBox myTextBox = new MyTextBox();
                    myTextBox.Xoa(txtMaPhamVi);
                    myTextBox.Xoa(txtTenPhamVi);
                }
                else
                {
                    Alert.Show("Ten pham vi nay da ton tai");
                    return;
                }
            }
            else
            {
                Alert.Show("Ma pham vi nay da ton tai");
                return;
            }
        }
    }
    protected void gvPhamVi_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvPhamVi.PageIndex = e.NewPageIndex;

        layDanhSachPhamVi();
    }
    protected void btnXoaPhamVi_Click(object sender, EventArgs e)
    {
        PhamViBUS pvBUS = new PhamViBUS();

        pvBUS.xoaPhamVi(gvPhamVi);
    }
    //*****

    //*****Table TrangThai*****
    protected void layDanhSachTrangThai()
    {
        TrangThaiBUS ttBUS = new TrangThaiBUS();
        gvTrangThai.DataSource = ttBUS.layDanhSachTrangThai();
        gvTrangThai.DataBind();
    }
    protected void btnThemTrangThai_Click(object sender, EventArgs e)
    {
        TrangThai ttDTO = new TrangThai();
        ttDTO.MaTrangThai = txtMaTrangThai.Text.ToUpper();
        ttDTO.TenTrangThai = txtTenTrangThai.Text.ToUpper();

        //Kiem tra rong
        if (ttDTO.MaTrangThai.ToString() == string.Empty)
        {
            Alert.Show("Ma trang thai khong duoc rong");
            return;
        }

        if (ttDTO.TenTrangThai.ToString() == string.Empty)
        {
            Alert.Show("Ten trang thai khong duoc rong");
            return;
        }

        //Them
        if (ttDTO.MaTrangThai.ToString() != string.Empty && ttDTO.TenTrangThai.ToString() != string.Empty)
        {
            TrangThaiBUS ttBUS = new TrangThaiBUS();

            if (ttBUS.kiemtraTonTaiMaTrangThai(ttDTO.MaTrangThai.ToUpper().ToString()) == false)
            {
                if (ttBUS.kiemtraTonTaiTenTrangThai(ttDTO.TenTrangThai.ToUpper().ToString()) == false)
                {
                    ttBUS.themTrangThai(ttDTO);

                    layDanhSachTrangThai();

                    MyTextBox myTextBox = new MyTextBox();
                    myTextBox.Xoa(txtMaTrangThai);
                    myTextBox.Xoa(txtTenTrangThai);
                }
                else
                {
                    Alert.Show("Ten trang thai nay da ton tai");
                    return;
                }
            }
            else
            {
                Alert.Show("Ma trang thai nay da ton tai");
                return;
            }
        }
    }
    protected void gvTrangThai_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTrangThai.PageIndex = e.NewPageIndex;

        layDanhSachTrangThai();
    }
    protected void btnXoaTrangThai_Click(object sender, EventArgs e)
    {
        TrangThaiBUS ttBUS = new TrangThaiBUS();

        ttBUS.xoaTrangThai(gvTrangThai);
    }
    //*****

    //*****Table TinhChat*****
    protected void layDanhSachTinhChat()
    {
        TinhChatBUS tcBUS = new TinhChatBUS();
        gvTinhChat.DataSource = tcBUS.layDanhSachTinhChat();
        gvTinhChat.DataBind();
    }
    protected void btnThemTinhChat_Click(object sender, EventArgs e)
    {
        TinhChat tcDTO = new TinhChat();
        tcDTO.MaTinhChat = txtMaTinhChat.Text.ToUpper();
        tcDTO.TenTinhChat = txtTenTinhChat.Text.ToUpper();

        //Kiem tra rong
        if (tcDTO.MaTinhChat.ToString() == string.Empty)
        {
            Alert.Show("Ma tinh chat khong duoc rong");
            return;
        }

        if (tcDTO.TenTinhChat.ToString() == string.Empty)
        {
            Alert.Show("Ten tinh chat khong duoc rong");
            return;
        }

        //Them
        if (tcDTO.MaTinhChat.ToString() != string.Empty && tcDTO.TenTinhChat.ToString() != string.Empty)
        {
            TinhChatBUS tcBUS = new TinhChatBUS();

            if (tcBUS.kiemtraTonTaiMaTinhChat(tcDTO.MaTinhChat.ToUpper().ToString()) == false)
            {
                if (tcBUS.kiemtraTonTaiTenTinhChat(tcDTO.TenTinhChat.ToUpper().ToString()) == false)
                {
                    tcBUS.themTinhChat(tcDTO);

                    layDanhSachTinhChat();

                    MyTextBox myTextBox = new MyTextBox();
                    myTextBox.Xoa(txtMaTinhChat);
                    myTextBox.Xoa(txtTenTinhChat);
                }
                else
                {
                    Alert.Show("Ten tinh chat nay da ton tai");
                    return;
                }
            }
            else
            {
                Alert.Show("Ma tinh chat nay da ton tai");
                return;
            }
        }
    }
    protected void gvTinhChat_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTinhChat.PageIndex = e.NewPageIndex;

        layDanhSachTinhChat();
    }
    protected void btnXoaTinhChat_Click(object sender, EventArgs e)
    {
        TinhChatBUS tcBUS = new TinhChatBUS();

        tcBUS.xoaTinhChat(gvTinhChat);
    }
    //*****

    //*****Table HinhThuc*****
    protected void layDanhSachHinhThuc()
    {
        HinhThucBUS htBUS = new HinhThucBUS();
        gvHinhThuc.DataSource = htBUS.layDanhSachHinhThuc();
        gvHinhThuc.DataBind();
    }
    protected void btnThemHinhThuc_Click(object sender, EventArgs e)
    {
        HinhThuc htDTO = new HinhThuc();
        htDTO.MaHinhThuc = txtMaHinhThuc.Text.ToUpper();
        htDTO.TenHinhThuc = txtTenHinhThuc.Text.ToUpper();

        //Kiem tra rong
        if (htDTO.MaHinhThuc.ToString() == string.Empty)
        {
            Alert.Show("Ma hinh thuc khong duoc rong");
            return;
        }

        if (htDTO.TenHinhThuc.ToString() == string.Empty)
        {
            Alert.Show("Ten hinh thuc khong duoc rong");
            return;
        }

        //Them
        if (htDTO.MaHinhThuc.ToString() != string.Empty && htDTO.TenHinhThuc.ToString() != string.Empty)
        {
            HinhThucBUS htBUS = new HinhThucBUS();

            if (htBUS.kiemtraTonTaiMaHinhThuc(htDTO.MaHinhThuc.ToUpper().ToString()) == false)
            {
                if (htBUS.kiemtraTonTaiTenHinhThuc(htDTO.TenHinhThuc.ToUpper().ToString()) == false)
                {
                    htBUS.themHinhThuc(htDTO);

                    layDanhSachHinhThuc();

                    MyTextBox myTextBox = new MyTextBox();
                    myTextBox.Xoa(txtMaHinhThuc);
                    myTextBox.Xoa(txtTenHinhThuc);
                }
                else
                {
                    Alert.Show("Ten hinh thuc nay da ton tai");
                    return;
                }
            }
            else
            {
                Alert.Show("Ma hinh thuc nay da ton tai");
                return;
            }
        }
    }
    protected void gvHinhThuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvHinhThuc.PageIndex = e.NewPageIndex;

        layDanhSachHinhThuc();
    }
    protected void btnXoaHinhThuc_Click(object sender, EventArgs e)
    {
        HinhThucBUS htBUS = new HinhThucBUS();

        htBUS.xoaHinhThuc(gvHinhThuc);
    }
    //*****

    //*****Table NoiPhatHanh*****
    protected void layDanhSachNoiPhatHanh()
    {
        NoiPhatHanhBUS nphBUS = new NoiPhatHanhBUS();
        gvNoiPhatHanh.DataSource = nphBUS.layDanhSachNoiPhatHanh();
        gvNoiPhatHanh.DataBind();
    }
    protected void btnThemNoiPhatHanh_Click(object sender, EventArgs e)
    {
        NoiPhatHanh nphDTO = new NoiPhatHanh();
        nphDTO.MaNoiPhatHanh = txtMaNoiPhatHanh.Text.ToUpper();
        nphDTO.TenNoiPhatHanh = txtTenNoiPhatHanh.Text.ToUpper();

        //Kiem tra rong
        if (nphDTO.MaNoiPhatHanh.ToString() == string.Empty)
        {
            Alert.Show("Ma noi phat hanh khong duoc rong");
            return;
        }

        if (nphDTO.TenNoiPhatHanh.ToString() == string.Empty)
        {
            Alert.Show("Ten noi phat hanh khong duoc rong");
            return;
        }

        //Them
        if (nphDTO.MaNoiPhatHanh.ToString() != string.Empty && nphDTO.TenNoiPhatHanh.ToString() != string.Empty)
        {
            NoiPhatHanhBUS nphBUS = new NoiPhatHanhBUS();

            if (nphBUS.kiemtraTonTaiMaNoiPhatHanh(nphDTO.MaNoiPhatHanh.ToUpper().ToString()) == false)
            {
                if (nphBUS.kiemtraTonTaiTenNoiPhatHanh(nphDTO.TenNoiPhatHanh.ToUpper().ToString()) == false)
                {
                    nphBUS.themNoiPhatHanh(nphDTO);

                    layDanhSachNoiPhatHanh();

                    MyTextBox myTextBox = new MyTextBox();
                    myTextBox.Xoa(txtMaNoiPhatHanh);
                    myTextBox.Xoa(txtTenNoiPhatHanh);
                }
                else
                {
                    Alert.Show("Ten noi phat hanh nay da ton tai");
                    return;
                }
            }
            else
            {
                Alert.Show("Ma noi phat hanh nay da ton tai");
                return;
            }
        }
    }
    protected void gvNoiPhatHanh_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvNoiPhatHanh.PageIndex = e.NewPageIndex;

        layDanhSachNoiPhatHanh();
    }
    protected void btnXoaNoiPhatHanh_Click(object sender, EventArgs e)
    {
        NoiPhatHanhBUS nphBUS = new NoiPhatHanhBUS();

        nphBUS.xoaNoiPhatHanh(gvNoiPhatHanh);
    }
}